import requests
import json
import pandas as pd
Mai_key = "2042af1de5ec0cfa53a793235a023dcb"

# 地理编码
def geocode(address,city=None,batch=None,sig=None)->dict:
    """获取地理编码"""
    url = 'https://restapi.amap.com/v3/geocode/geo?parameters'
    params={
        'key': key_dl,
        'address':address,
        'city':city,
        'batch':batch,
        'sig':sig, 
    }
    response = requests.get(url,params=params)
    data = response.json()
    return data
广东省 = geocode(address='广东省')
print(广东省)
df_广东省地理编码 = pd.json_normalize(广东省['geocodes'])
display(df_广东省地理编码)
广东省地理编码 = 广东省['geocodes'][0]['location']
print("广东省地理编码:",广东省地理编码)

# 逆地理编码
def regeocode(location,poitype=None,radius=None,extensions="base",batch=False,roadlevel=None,sig=None,homeorcorp=None)->dict:
    """获取逆地理编码"""
    url = 'https://restapi.amap.com/v3/geocode/regeo?parameters'
    params={
        'key': key_dl,
        'location':location,
        'poitype':poitype,
        'radius':radius,
        'extensions':extensions,
        'batch':batch,
        'roadlevel':roadlevel,
        'homeorcorp':homeorcorp,
        'sig':sig,
        'output':'json'
    }
    response = requests.get(url,params=params)
    data = response.json()
    return data

中大南方逆地理编码 = regeocode(中大南方地理编码)
print(中大南方逆地理编码)
df_中大南方逆地理编码 = pd.json_normalize(中大南方逆地理编码).T
display(df_中大南方逆地理编码)

# A-2 Extra 全部逆地理编码分析
中大南方逆地理编码_all = regeocode(中大南方地理编码,extensions='all',radius=300)
print(中大南方逆地理编码_all)
df_中大南方逆地理编码_all = pd.json_normalize(中大南方逆地理编码_all)
display(df_中大南方逆地理编码_all)

# 路径规划
# 步行
def walking(origin,destination,sig=None)->dict:
    url = 'https://restapi.amap.com/v3/direction/walking?parameters'
    params={
        'key':key_dl,
        'origin':origin,
        'destination':destination,
        'output':'json'
    }
    response = requests.get(url,params=params)
    data = response.json()
    return data
从化客运站 = geocode('广东省广州市从化区从化客运站')
从化客运站_location = 从化客运站['geocodes'][0]['location']
中大南方_location = 中大南方['geocodes'][0]['location']
print("(起点)中大南方_location:",中大南方_location,"(终点)从化客运站_location:",从化客运站_location)
中大南方_从化客运站 = walking(中大南方_location,从化客运站_location)
df_步行路径规划 = pd.json_normalize(中大南方_从化客运站["route"]["paths"][0]['steps'])
print(中大南方_从化客运站)
display(df_步行路径规划)
df_步行路径规划["instruction"]

# 搜索POI
# 关键字搜索
def place_text(keywords,types,city=None,citylimit=None,children=None,page=None,extensions='base',sig=None)->dict:
    url = 'https://restapi.amap.com/v3/place/text?parameters'
    params={
        'key':key_dl,
        'keywords':keywords,
        'types':types,
        'city':city,
        'citylimit':citylimit,
        'children':children,
        'page':page,
        'extensions':extensions,
        'output':'json'
    }
    response = requests.get(url,params=params)
    data = response.json()
    return data
广州_中学 = place_text(keywords=None,types='中学',city="广州市",children=1,extensions='all',page=3)
print(广州_中学)
df_广州_中学 = pd.json_normalize(广州_中学["pois"])
df_广州_中学

# 周边搜索
# F-1 
def place_around(location,keywords=None,types=None,city=None,redius=None,sortrule=None,offset=None,page=None,extensions='base',sig=None)->dict:
    url = 'https://restapi.amap.com/v3/place/around?parameters'
    params={
        'key':key_dl,
        'keywords':keywords,
        'location':location,
        'types':types,
        'city':city,
        'redius':redius,
        'sortrule':sortrule,
        'offset':offset,
        'page':page,
        'extensions':extensions,
        'output':'json'
    }
    response = requests.get(url,params=params)
    data = response.json()
    return data
中大南方_周边POI = place_around(中大南方_location)
中大南方_周边POI
df_中大南方_周边POI = pd.json_normalize(中大南方_周边POI['pois'])
df_中大南方_周边POI

# IP定位
def ip_around(key,ip="192.168.43.139",sig=None)->dict:
    ip_url = "https://restapi.amap.com/v3/ip?parameters"
    params = {
        "key":key_dl,
        "ip":ip,
        "sig":sig,
        "output":"json"
    }
    response = requests.get(ip_url,params=params)
    data = response.json()
    return data
我的ip = ip_around('192.168.43.139')
我的ip

# 静态地图
from PIL import Image
from io import BytesIO
def staticmap(location,zoom,size=None,scale=1,markers=None,labels=None,paths=None,traffic=0,page=None,sig=None)->dict:
    url = 'https://restapi.amap.com/v3/staticmap?parameters'
    params={
        'key':key_dl,
        'location':location,
        'zoom':zoom,
        'size':size,
        'scale':scale,
        'markers':markers,
        'labels':labels,
        'paths':paths,
        'traffic':traffic,
        'sig':sig,
        'output':'json'
    }
    response = requests.get(url,params=params)
    data = Image.open(BytesIO(response.content))
    return data
staticmap(location=中大南方_location,zoom=16)

# 输入提示
def tips(key,keywords="麦当劳",city="440000",location="113.266530,23.132191",citylimit="true",datatype="all",sig=None) ->dict:
    tips_url = "https://restapi.amap.com/v3/assistant/inputtips?parameters"
    params = {
        "key":key_dl,
        "keywords":keywords,
        "city":city,
        "location":location,
        "citylimit":citylimit,
        "datatype":datatype,
        "sig":sig,
        "output":"json"
    }
    response = requests.get(tips_url,params=params)
    data = response.json()
    return data
麦当劳 = tips("440000")
麦当劳


